package com.sun.dao.impl;

import com.sun.dao.BaseDao;
import com.sun.dao.MovieDao;
import com.sun.entity.Movie;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author SunRui
 * @description 实现类
 * @date 2024/10/9 15:08
 */
public class MovieDaoImpl extends BaseDao implements MovieDao {
    @Override
    public List<Movie> selectMovie(int page) {
        List<Movie> movieList = new ArrayList<Movie>(); // 创建一个 List 集合接收数据
        String sql = " select movieId,movieName,movieType,price,recommend,startTime,endTime,votes " +
                " from movie" +
                " LIMIT ?,2";
        ResultSet resultSet = executeQuery(sql,page);
        if (resultSet != null){
            try{
                while (resultSet.next()){
                    Movie movie = new Movie();
                    movie.setMovieId(resultSet.getInt("movieId"));
                    movie.setMovieName(resultSet.getString("movieName"));
                    movie.setMovieType(resultSet.getString("movieType"));
                    movie.setPrice(resultSet.getFloat("price"));
                    movie.setRecommend(resultSet.getInt("recommend"));
                    movie.setStartTime(resultSet.getDate("startTime"));
                    movie.setEndTime(resultSet.getDate("endTime"));
                    movie.setVotes(resultSet.getInt("votes"));
                    movieList.add(movie);
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                closedAll(resultSet);
            }
        }
        return movieList;
    }

    @Override
    public int selectAllNumber() {
        String sql = "select COUNT(*) from movie";
        ResultSet resultSet = executeQuery(sql);
        int i = -1;
        if (resultSet != null){
            try{
                while (resultSet.next()){
                   i = resultSet.getInt("COUNT(*)");
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                closedAll(resultSet);
            }
        }
        return i;
    }

    @Override
    public int selectMovieByMovieName(String movieName) {
        String sql = "select COUNT(*) from movie where movieName = ?";
        ResultSet resultSet = executeQuery(sql,movieName);
        int j = -1;
        if (resultSet != null){
            try{
                while (resultSet.next()){
                    j = resultSet.getInt("COUNT(*)");
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                closedAll(resultSet);
            }
        }
        return j;
    }

    @Override
    public int addMovie(Movie movie) {
        String sql = "insert into movie(movieName,movieType,price,recommend,startTime,endTime,votes)" +
                " values(?,?,?,?,?,?,?)";
        return exceuteUpdate(sql,movie.getMovieName(),movie.getMovieType(),movie.getPrice(),movie.getRecommend(),
                movie.getStartTime(), movie.getEndTime(),movie.getVotes());
    }
}
